package com.zsix.system.strategy.jump;

import com.zsix.system.service.manager.ProcessTaskServiceManager;
import com.zsix.common.dto.SendActionDto;
import com.zsix.common.dto.controllerParams.ProcessTaskParams;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class JumpSatpStep implements JumpStrategy {
    @Autowired //任务服务
    private TaskService taskService;
    @Autowired
    private ProcessTaskServiceManager processTaskServiceManager;

    /**
     * 如果与上一步处理人相同，执行跳转
     * @param username
     * @param processTaskParams
     * @param toUsers
     * @return
     */
    @Override
    public SendActionDto Do(String username, ProcessTaskParams processTaskParams
            ,String toUsers){
        SendActionDto sendActionDto=new SendActionDto();
        //如果下一步接收人只有一个
        if(!toUsers.contains(",")){
            if(toUsers.equals(username)) {
                //先拾取任务
                taskService.setAssignee(processTaskParams.getTaskId(),username);
                //执行发送
                sendActionDto = processTaskServiceManager.sendWork(processTaskParams, username);
            }
        }
        else{
            Boolean isHaveUser=false;
            //如果有多个人，判断是否包括当前登陆人
            String[] users=toUsers.split(",");
            for(String user:users){
                if(user.equals(username)){
                    isHaveUser=true;
                    break;
                }
            }
            //如果包括，执行跳转
            if(isHaveUser) {
                taskService.setAssignee(processTaskParams.getTaskId(),username);
                sendActionDto = processTaskServiceManager.sendWork(processTaskParams, username);
            }
        }
        return sendActionDto;
    }

    /**
     * 返回服务类型
     * @return
     */
    @Override
    public String getType(){
        return "satpStep";
    }
}
